package com.inf8405.db;

import java.util.ArrayList;
import java.util.List;

import com.module.StationFinder.Station;

import android.R.integer;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DAOStations {

  // Database fields
  private SQLiteDatabase database;
  private SQLiteHelper dbHelper;
  private String[] allColumns = { SQLiteHelper.COLUMN_ID,
		  SQLiteHelper.COLUMN_NAME ,
		  SQLiteHelper.COLUMN_REFERENCE ,
		  SQLiteHelper.COLUMN_ICON ,
		  SQLiteHelper.COLUMN_VICINITY ,
		  SQLiteHelper.COLUMN_GEOMETRY_LAT ,
		  SQLiteHelper.COLUMN_GEOMETRY_LONG ,
		  SQLiteHelper.COLUMN_FORMATTED_ADDRESS ,
		  SQLiteHelper.COLUMN_FORMATTED_PHONE_NUMBER ,
		  SQLiteHelper.COLUMN_PRICE ,
		  SQLiteHelper.COLUMN_PRICE_DATE };

  
  
  public DAOStations(Context context) {
    dbHelper = new SQLiteHelper(context);
  }

  public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
  }

  public void close() {
    dbHelper.close();
  }

  public long InsertInfoStationDB(String id, String name, 
		  String reference, String icon, String vicinity,
		  String geometry_lat, String geometry_long, String formated_address, 
		  String formatted_phone_number, String price, String price_date) 
  	{
	    ContentValues values = new ContentValues();
	    values.put(SQLiteHelper.COLUMN_ID, id);
	    values.put(SQLiteHelper.COLUMN_NAME, name);
	    values.put(SQLiteHelper.COLUMN_REFERENCE, reference);
	    values.put(SQLiteHelper.COLUMN_ICON, icon);
	    values.put(SQLiteHelper.COLUMN_VICINITY, vicinity);
	    values.put(SQLiteHelper.COLUMN_GEOMETRY_LAT, geometry_lat);
	    values.put(SQLiteHelper.COLUMN_GEOMETRY_LONG, geometry_long);
	    values.put(SQLiteHelper.COLUMN_FORMATTED_ADDRESS, formated_address);
	    values.put(SQLiteHelper.COLUMN_FORMATTED_PHONE_NUMBER, formatted_phone_number);
	    values.put(SQLiteHelper.COLUMN_PRICE, price);
	    values.put(SQLiteHelper.COLUMN_PRICE_DATE, price_date);
	    
	    long insertId = database.insert(SQLiteHelper.TABLE_NAME, null, values);
		return insertId;
	  }
  
  public Station PickStation(String id) {
    Cursor cursor = database.query(SQLiteHelper.TABLE_NAME,
        allColumns, SQLiteHelper.COLUMN_ID + " = " + id +"", null,
        null, null, null);
    cursor.moveToFirst();
    Station newStation = cursorToStation(cursor);
    cursor.close();
    return newStation;
  }
  public boolean ExistStation(String id) {
	  String where = SQLiteHelper.COLUMN_ID + " = '" + id +"'";
	  //Log.w("MED","where = "+ where);
	    Cursor cursor = database.query(SQLiteHelper.TABLE_NAME,
	        allColumns, where, null,
	        null, null, null);
	    boolean existsta = cursor.moveToFirst();
	    cursor.close();
	    return existsta;
	  }

  public void deleteStation(Station station) {
    String id = station.id;
    database.delete(SQLiteHelper.TABLE_NAME, SQLiteHelper.COLUMN_ID
        + " = " + id, null);
  }
  
  public void deleteTableStations(String table) {
	    database.delete(table, null, null);
	  }
  
  public void deleteStation(String id) {
	   String where = SQLiteHelper.COLUMN_ID
		        + " = '" + id+"'";
	    int ii = database.delete(SQLiteHelper.TABLE_NAME, where, null);
	  }

  public List<Station> getAllStations() {
    List<Station> stations = new ArrayList<Station>();

    Cursor cursor = database.query(SQLiteHelper.COLUMN_NAME,
        allColumns, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
    	Station station = cursorToStation(cursor);
    	stations.add(station);
      cursor.moveToNext();
    }
    cursor.close();
    return stations;
  }

  private Station cursorToStation(Cursor cursor) {
	  Station station = new Station();
	  station.id = cursor.getString(0);
	  station.name = cursor.getString(1);
	  station.reference = cursor.getString(2);
	  station.icon = cursor.getString(3);
	  station.vicinity = cursor.getString(4);
	  station.geometry.location.lat = cursor.getDouble(5);
	  station.geometry.location.lng = cursor.getDouble(6);
	  station.formatted_address = cursor.getString(7);
	  station.formatted_phone_number = cursor.getString(8);
	  //station.price = cursor.getString(8);
	  //station.price_date = cursor.getString(9);
	  
    return station;
  }
}